package com.knowledge.controller;


import com.knowledge.common.aop.LogAnnotation;
import com.knowledge.common.aop.TokenValidate;
import com.knowledge.common.constant.GlobalConstant;
import com.knowledge.common.enums.ErrorCode;
import com.knowledge.dao.entity.SysUser;
import com.knowledge.service.ArticleService;
import com.knowledge.utils.UserThreadLocal;
import com.knowledge.vo.Result;
import com.knowledge.vo.params.ArticleParam;
import com.knowledge.vo.params.PageParams;
import com.knowledge.vo.params.ShareParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author zzz
 * @create 2022-02-10 16:16
 */
@RestController
@RequestMapping("/articles")
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    @PostMapping
    @LogAnnotation(module="文章",operator="获取文章列表")
    public Result listArticle(@RequestBody PageParams pageParams) {
        //是否收藏列表
        Integer favorite = pageParams.getFavorite();
        if (GlobalConstant.YES.equals(favorite)){
            //是查询收藏校验是否登录
            SysUser sysUser = UserThreadLocal.get();
            if (sysUser == null){
                return Result.fail(ErrorCode.PARAMS_ERROR);
            }
        }
        //文章列表
        return articleService.listArticle(pageParams);
    }

    /**
     * 首页最热文章
     * @return
     */
    @PostMapping("/hot")
    public Result hotArticle(){
        int limit = 5;
        return articleService.hotArticle(limit);
    }

    /**
     * 首页最新文章
     * @return
     */
    @PostMapping("/new")
    public Result newArticle(){
        int limit = 5;
        return articleService.newArticle(limit);
    }

    /**
     * 首页文章归档
     * @return
     */
    @PostMapping("/listArchives")
    public Result listArchives(){
        return articleService.listArchives();
    }
//    将文章信息显示出来
    @PostMapping("view/{id}")
    public Result viewArticleById(@PathVariable("id") long id){
        return articleService.findArticleById(id);
    }

    @TokenValidate
    @PostMapping("/publish")
    public Result publishArticle(@RequestBody ArticleParam articleParam){
       return articleService.publishArticle(articleParam);
    }

    @TokenValidate
    @PostMapping("/top/{id}")
    public Result pushTop(@PathVariable("id") long id) {
        return articleService.pushTop(id);
    }

    @TokenValidate
    @PostMapping("/delete/{id}")
    public Result delete(@PathVariable("id") long id) {
        return articleService.delete(id);
    }

     /**
     　* @description: 文章分享
     　* @param ${tags}
     　* @return ${return_type}
     　* @throws
     　* @author zzz
     　* @date 2022-03-05 23:03
     */
    @TokenValidate
    @PostMapping("/share")
    public Result share(@RequestBody ShareParam shareParam) {
        if (shareParam.getFriendUserId() == null || shareParam.getArticleId() == null){
            return Result.fail(ErrorCode.PARAMS_ERROR);
        }
        SysUser sysUser = UserThreadLocal.get();
        if (sysUser.getId().equals(shareParam.getFriendUserId())){
            return Result.fail(ErrorCode.SHARE_ERROR);
        }
        //设置用户ID
        shareParam.setUserId(sysUser.getId());
        shareParam.setUserName(sysUser.getNickname());
        Integer res = articleService.addShareArticle(shareParam);
        return GlobalConstant.YES.equals(res) ? Result.success()
                : Result.fail(ErrorCode.SHARE_ERROR_ARTICLE_DELETE);
    }
}
